#include "cclientcfgdlg.h"
#include "cpublicdata.h"
#include "cglobal.h"
#include "cclientdata.h"

CClientCfgDlg::CClientCfgDlg(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("客户端配置"));
    m_nType = 0;
    m_pClient = nullptr;
    initUI();
    initConnect();
}

CClientCfgDlg::~CClientCfgDlg()
{}

void CClientCfgDlg::setData(QString &sName, QString &sIp, int &nPort,int nPointCount)
{
	ui.lineEditClientIP->setText(sIp);
	ui.lineEditClientName->setText(sName);
	ui.lineEditClientPort->setText(QString::number(nPort));
	ui.lineEditPointCount->setText(QString::number(nPointCount));
	ui.lineEditClientIP->setEnabled(false);
    ui.lineEditClientPort->setEnabled(false);
}

void CClientCfgDlg::setClient(CClientData *pClient)
{
    ui.lineEditClientIP->setText(pClient->m_sIp);
    ui.lineEditClientName->setText(pClient->m_sName);
    ui.lineEditClientPort->setText(QString::number(pClient->m_nPort));
    ui.lineEditPointCount->setText(QString::number(pClient->m_nPointCount));
    ui.checkBoxClientState->setChecked(pClient->m_bEnabled);
    m_pClient = pClient;
}

void CClientCfgDlg::initUI()
{
    ui.checkBoxClientState->setChecked(true);
    ui.checkBoxClientState->setText(A2T("启用"));
    bool bServerRunning = CPublicData::instance().m_bServerRuninng;
    if (true == bServerRunning)
    {
        QLabel* pLabel = new QLabel(A2T("服务端运行中，配置前请先停止服务端"));
        pLabel->setStyleSheet("color:red;font:bold;");
        pLabel->setAlignment(Qt::AlignHCenter);
        ui.verticalLayout->insertWidget(0, pLabel);
        ui.widgetSettings->setEnabled(false);
        ui.pushButtonOK->setEnabled(false);
    }
    else
    {
        ui.widgetSettings->setEnabled(true);
        ui.lineEditClientIP->setEnabled(false);
        ui.pushButtonOK->setEnabled(true);
    }
}

void CClientCfgDlg::initConnect()
{
	connect(ui.pushButtonOK, &QPushButton::clicked, this, &CClientCfgDlg::slotPushButtonOK);
    connect(ui.pushButtonClose, &QPushButton::clicked, this, &CClientCfgDlg::slotPushButtonClose);
    connect(ui.checkBoxClientState, &QCheckBox::toggled, this, [=](bool bChecked){
        ui.checkBoxClientState->setText(bChecked ? A2T("启用") : A2T("停用"));
    });
}

void CClientCfgDlg::slotPushButtonOK()
{
	if (ui.lineEditClientIP->text() == "" || ui.lineEditClientName->text() == "" || ui.lineEditClientPort->text() == "")
	{
		messageBox(0, A2T("请填写完毕后再进行确认"));
		return;
	}
	QString sName = ui.lineEditClientName->text();
	QString sIp = ui.lineEditClientIP->text();
	int nPort = ui.lineEditClientPort->text().toInt();
    int nPointCount = ui.lineEditPointCount->text().toInt();
    bool bEnabled = ui.checkBoxClientState->isChecked();
	if (m_nType == 0)//增加
	{
		CPublicData::instance().m_pMutex->lock();
        //if (CPublicData::instance().m_mapClient.contains(sIp + ":" + QString::number(nPort)))
        if (CPublicData::instance().m_mapClient.contains(sIp))
        {
			CPublicData::instance().m_pMutex->unlock();
			messageBox(0, A2T("已经存在此客户端的配置信息，请勿重复添加"));
			return;
		}
		CClientData* pClient = new CClientData();
        //pClient->m_sGuid = sIp + ":" + QString::number(nPort);
        pClient->m_sGuid = sIp;
		pClient->m_sName = sName;
		pClient->m_sIp = sIp;
        //pClient->m_nPort = nPort;
        pClient->m_nPointCount = nPointCount;
        pClient->m_bEnabled = bEnabled;
        CPublicData* pData = &CPublicData::instance();
        pData->m_mapClient.insert(pClient->m_sGuid, pClient);
        pData->m_listClient.append(pClient);
        pData->saveIni();
        pData->m_pMutex->unlock();
        messageBox(0, A2T("新增客户端成功"));
		this->close();
		return;
	}
	else if (m_nType == 1)//修改
	{
        if (m_pClient == nullptr)
        {
            messageBox(0, A2T("客户端异常，修改失败"));
            return;
        }
        CPublicData::instance().m_pMutex->lock();
        if (CPublicData::instance().m_mapClient.contains(sIp) && m_pClient->m_sGuid != sIp)
        {
            CPublicData::instance().m_pMutex->unlock();
            messageBox(0, A2T("已经存在此客户端的配置信息，请勿重复添加"));
            return;
        }
        m_pClient->m_pMutex->lock();
        m_pClient->m_sGuid = sIp;
        m_pClient->m_sIp = sIp;
        m_pClient->m_sName = sName;
        m_pClient->m_nPointCount = nPointCount;
        m_pClient->m_bEnabled = bEnabled;
        m_pClient->m_pMutex->unlock();
        CPublicData::instance().m_pMutex->unlock();
        messageBox(0, A2T("修改客户端成功"));
        this->close();
        return;
	}
}

void CClientCfgDlg::slotPushButtonClose()
{
	this->close();
}
